-- slime_global_weaken
-- create d by zhuangsl
-- 通用的削弱目标类技能(对所有怪物起效,冈布奥主动技用)

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[5] > 0 then
            effectRound = round + para[5] + 1;
        end

        local targets = {};
        local grids = DungeonM.getCurrentDungeon();
        for _, grid in pairs(grids) do
            if  grid:isMonster() and
                grid:isOpened() and
                not grid.monster:isDead() then
                table.insert(targets, grid.monster);
            end
        end

        local value = para[4];
        -- 具体数值，某些技能要求数值是变化的，想不出好办法，先单独拉出来算…
        if para[2] == PropM.getPropId("reduce_attrib") then
            -- 远古咆哮
            local attack = source:queryAttrib("attack");
            value = math.min(attack * para[4] + 100, 500);
        end

        -- local arr = { para[2], para[3], value, effectRound, }
        local condition = {
            ["id"] = para[2],
            ["value"] = value,
            ["end_round"] = effectRound,
        };
        if "1" ~= para[3] then
            condition["key"] = para[3];
        end

        if para[6] == 1 then
            -- 需要计算禁锢抗性的
            for _, target in pairs(targets) do
                -- 需要判断是否存在禁锢抗性
                local prop = PropM.combine(target, "durance_resist", 1);
                if PropM.apply(prop, 1) > 0 then
                    -- 只提示BOSS的
                    if target:isBoss() and not target:isDead() then
                        if prop[4] == -1 then
                            doAlert(getLocStr("no_durance2"));
                        else
                            doAlert(string.format(getLocStr("no_durance"), (prop[4] - round)));
                        end
                    end
                else
                    -- 如果不免疫该技能，则附加状态
                    if SkillM.immunitySkill(source, target, skillId) then
                        SkillM.getSequence(target):immunity(source, target, skillId);
                    else
                        CombatStatusM.applyStatus(target, para[1], condition);
                    end
                end
            end
        else
            -- 不计算禁锢抗性的
            for _, target in pairs(targets) do
                -- 如果不免疫该技能，则附加状态
                if SkillM.immunitySkill(source, target, skillId) then
                    SkillM.getSequence(target):immunity(source, target, skillId);
                else
                    CombatStatusM.applyStatus(target, para[1], condition);
                end
            end
        end
        -- 返回作用目标
        return targets;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), arr[3], tonumber(arr[4]), tonumber(arr[5]), tonumber(arr[6]) or -1, };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 数值
        local value = math.abs(para[4]);
        if para[2] == PropM.getPropId("reduce_attrib") then
            -- 远古咆哮
            local attack = source:queryAttrib("attack");
            value = math.min(attack * para[4] + 100, 500);
            -- 这里是千分比，显示要除以10
            value = math.modf(value / 10);
        end

        desc = string.gsub(desc, "{weaken}", value);

        -- 千分比类的
        value = math.modf(value / 10);
        desc = string.gsub(desc, "{weaken_per}", value);

        -- 回合数的显示
        local round = para[5];
        if round > 0 then
            desc = string.gsub(desc, "{round}", round);
        end

        return desc;
    end,
};
